import http from '@/utils/http';

class ApiService {
    // 用户相关
    async login(username, password) {
        try {
            const response = await http.post('/user/login', { username, password });
            return response;
        } catch (error) {
            console.error('登录失败:', error);
            throw error;
        }
    }

    async register(userData) {
        try {
            const response = await http.post('/user/register', userData);
            return response;
        } catch (error) {
            console.error('注册失败:', error);
            throw error;
        }
    }

    // 社区相关
    async getPosts() {
        try {
            const response = await http.get('/post');
            return response;
        } catch (error) {
            console.error('获取帖子列表失败:', error);
            throw error;
        }
    }

    async createPost(postData) {
        try {
            const response = await http.post('/post', postData);
            return response;
        } catch (error) {
            console.error('创建帖子失败:', error);
            throw error;
        }
    }

    async likePost(postId) {
        try {
            const response = await http.post(`/like/${postId}`);
            return response;
        } catch (error) {
            console.error('点赞失败:', error);
            throw error;
        }
    }

    async commentPost(postId, content) {
        try {
            const response = await http.post(`/comment`, { postId, content });
            return response;
        } catch (error) {
            console.error('评论失败:', error);
            throw error;
        }
    }

    // 资料库相关
    async getPandas() {
        try {
            const response = await http.get('/panda');
            return response;
        } catch (error) {
            console.error('获取熊猫列表失败:', error);
            throw error;
        }
    }

    async getPandaBases() {
        try {
            const response = await http.get('/pandabasis');
            return response;
        } catch (error) {
            console.error('获取熊猫基地列表失败:', error);
            throw error;
        }
    }

    // 商城相关
    async getProducts() {
        try {
            const response = await http.get('/product');
            return response;
        } catch (error) {
            console.error('获取商品列表失败:', error);
            throw error;
        }
    }

    async getCategories() {
        try {
            const response = await http.get('/category');
            return response;
        } catch (error) {
            console.error('获取分类列表失败:', error);
            throw error;
        }
    }

    async addToCart(productId, quantity) {
        try {
            const response = await http.post('/cartitem', { productId, quantity });
            return response;
        } catch (error) {
            console.error('添加到购物车失败:', error);
            throw error;
        }
    }

    async getCartItems() {
        try {
            const response = await http.get('/cartitem');
            return response;
        } catch (error) {
            console.error('获取购物车失败:', error);
            throw error;
        }
    }
}

export default new ApiService(); 